package com.jie.langlangmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.dao.SmsFlashPromotionProductRelationDao;
import com.jie.langlangmall.dto.SmsFlashPromotionProduct;
import com.jie.langlangmall.mapper.SmsFlashPromotionProductRelationMapper;
import com.jie.langlangmall.model.SmsFlashPromotionProductRelation;
import com.jie.langlangmall.model.SmsFlashPromotionProductRelationExample;
import com.jie.langlangmall.service.SmsFlashPromotionProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/26
 * @Email 3217998214@qq.com
 * @Description 限时购商品关联管理
 */
@Service
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {

    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;

    @Autowired
    private SmsFlashPromotionProductRelationDao relationDao;

    @Override
    public long getCount(Long flashPromotionId, Long flashPromotionSessionId) {
        SmsFlashPromotionProductRelationExample example = new SmsFlashPromotionProductRelationExample();
        example.createCriteria()
                .andFlashPromotionIdEqualTo(flashPromotionId)
                .andFlashPromotionSessionIdEqualTo(flashPromotionSessionId);
        return relationMapper.countByExample(example);
    }

    @Override
    @Transactional
    public void create(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation relation : relationList) {
            relationMapper.insert(relation);
        }
    }

    /**
     * 更新
     *
     * @param id
     * @param relation
     */
    @Override
    public void update(Long id, SmsFlashPromotionProductRelation relation) {
        relation.setId(id);
        relationMapper.updateByPrimaryKey(relation);
    }

    @Override
    public void delete(Long id) {
        relationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SmsFlashPromotionProductRelation getItem(Long id) {
        return relationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SmsFlashPromotionProduct> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return relationDao.getList(flashPromotionId, flashPromotionSessionId);
    }

}
